//
//  GradientBorderLabel.swift
//  STest
//
//  Created by wang tiao on 2025/9/13.
//


import UIKit

//import UIKit
//
//class GradientBorderLabel: UILabel {
//    
//    // 渐变颜色数组
//    var gradientColors: [UIColor] = [UIColor.systemBlue, UIColor.systemPink] {
//        didSet { updateGradient() }
//    }
//    
//    // 边框宽度
//    var borderWidth: CGFloat = 2 {
//        didSet { shapeLayer.lineWidth = borderWidth; setNeedsLayout() }
//    }
//    
//    private let gradientLayer = CAGradientLayer()
//    private let shapeLayer = CAShapeLayer()
//    
//    override init(frame: CGRect) {
//        super.init(frame: frame)
//        setupUI()
//    }
//    
//    required init?(coder: NSCoder) {
//        super.init(coder: coder)
//        setupUI()
//    }
//    
//    private func setupUI() {
//        textAlignment = .center
//        layer.addSublayer(gradientLayer)
//        gradientLayer.mask = shapeLayer
//        backgroundColor = .clear
//        clipsToBounds = false
//        updateGradient()
//    }
//    
//    private func updateGradient() {
//        gradientLayer.colors = gradientColors.map { $0.cgColor }
//        gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
//        gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
//        setNeedsLayout()
//    }
//    
//    override func layoutSubviews() {
//        super.layoutSubviews()
//        gradientLayer.frame = bounds
//        let path = UIBezierPath(roundedRect: bounds.insetBy(dx: borderWidth / 2, dy: borderWidth / 2),
//                                cornerRadius: bounds.height / 2)
//        shapeLayer.path = path.cgPath
//        shapeLayer.fillColor = UIColor.clear.cgColor
//        shapeLayer.strokeColor = UIColor.black.cgColor
//        shapeLayer.lineWidth = borderWidth
//    }
//}


import UIKit

class GradientBorderLabel: UILabel {
    
    /// 渐变边框颜色
    var gradientColors: [UIColor] = [UIColor.systemBlue, UIColor.systemPink] {
        didSet { updateGradient() }
    }
    
    /// 边框宽度
    var borderWidth: CGFloat = 2 {
        didSet { shapeLayer.lineWidth = borderWidth; setNeedsLayout() }
    }
    
    /// 边框颜色（用于渐变）
    var borderStrokeColor: UIColor = .black {
        didSet { shapeLayer.strokeColor = borderStrokeColor.cgColor }
    }
    
    private let gradientLayer = CAGradientLayer()
    private let shapeLayer = CAShapeLayer()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    private func setupUI() {
        textAlignment = .center
        layer.addSublayer(gradientLayer)
        gradientLayer.mask = shapeLayer
        clipsToBounds = false
        updateGradient()
    }
    
    private func updateGradient() {
        gradientLayer.colors = gradientColors.map { $0.cgColor }
        gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
        gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
        setNeedsLayout()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        gradientLayer.frame = bounds
        
        let path = UIBezierPath(roundedRect: bounds.insetBy(dx: borderWidth / 2, dy: borderWidth / 2),
                                cornerRadius: bounds.height / 2)
        shapeLayer.path = path.cgPath
        shapeLayer.fillColor = UIColor.clear.cgColor   // 内部透明，由 UILabel 的 backgroundColor 决定
        shapeLayer.strokeColor = borderStrokeColor.cgColor
        shapeLayer.lineWidth = borderWidth
    }
}
